package com.shfl.Controller;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import com.shfl.Util.ExcelUtil;
import com.shfl.Util.MapObjUtil;
import com.shfl.Util.POIUtil;
import com.shfl.model.WokingLog;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Window;

public class MainController2 implements Initializable {

	@FXML
	private Button selectFile;

	@FXML
	private TextField showFileName;

	@FXML
	private Button exportFile;

	@FXML
	private TextField exportPath;

	@FXML
	private TableView tview;

	@FXML
	private TableColumn c1;

	@FXML
	private TableColumn c2;

	@FXML
	private TableColumn c3;

	@FXML
	private TableColumn c4;

	@FXML
	private TableColumn c5;

	@FXML
	private TableColumn c6;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void initialize(URL location, ResourceBundle resources) {
		// 选择文件
		this.selectFile.setOnAction(event -> {
			FileChooser fileChooser = new FileChooser();
			// 只能够获取的文件
			fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("XLSX", "*.xlsx"),
					new FileChooser.ExtensionFilter("XLS", "*.xls"));
			Window window = ((Node) event.getSource()).getScene().getWindow();
			File file = fileChooser.showOpenDialog(window);
			if (file != null) {
				this.showFileName.setText(file.getAbsolutePath());
				List<String[]> list = new ArrayList<String[]>();
				try {
					list = POIUtil.readExcel(file);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(list.size());
			}
		});

		ObservableList<WokingLog> list = FXCollections.observableArrayList();

		WokingLog wokingLog = new WokingLog();
		// wokingLog.setUserName("test");
		// wokingLog.setNo("121");
		// wokingLog.setLackHours("100");
		c1.setCellValueFactory(new PropertyValueFactory("userName"));// 映射
		c2.setCellValueFactory(new PropertyValueFactory("No"));
		c3.setCellValueFactory(new PropertyValueFactory("totalHours"));
		c4.setCellValueFactory(new PropertyValueFactory("lackHours"));
		c5.setCellValueFactory(new PropertyValueFactory("No"));
		c6.setCellValueFactory(new PropertyValueFactory("No"));

		List<WokingLog> wokingLogList = new LinkedList<WokingLog>();
		List<Map<String, Object>> tempList = new LinkedList<Map<String, Object>>();
		for (int i = 0; i < 10; i++) {
			wokingLogList.add(wokingLog);
			tempList.add(MapObjUtil.object2Map(wokingLog));
		}
		// list.add(wokingLog); // list添加值对象
		list.addAll(wokingLogList);
		tview.setItems(list); // tableview添加list

		// 选择导出的文件架
		this.exportFile.setOnAction(event -> {
			DirectoryChooser chooser = new DirectoryChooser();
			Window window = ((Node) event.getSource()).getScene().getWindow();
			File file = chooser.showDialog(window);
			if (file != null && file.exists() && file.isDirectory()) {
				this.exportPath.setText(file.getAbsolutePath());
				// List dataKey
				String[] dataKeyArry = { "userName", "No", "totalHours", "lackHours" };
				// List dataTitle
				String[] dataTitleArry = { "员工姓名", "员工编号", "总工时", "缺勤工时" };
				// 生成Excel
				Map rMap = ExcelUtil.buildExcel(file.getAbsolutePath(), Arrays.asList(dataKeyArry),
						Arrays.asList(dataTitleArry), tempList, "会员奖励金");
			}
		});

	}

	// public void showDateTime(ActionEvent event) {
	// System.out.println("Button Clicked!");
	//
	// Date now= new Date();
	//
	// DateFormat df = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
	// String dateTimeString = df.format(now);
	// // fx:id对了就可以直接使用对象，直接setText不对应，就会得到null，直接报错
	// showFileName.setText(dateTimeString);
	// }

}
